#include "sourcemanage.h"

QMutex SourceManage::mutex;
SourceManage* SourceManage::instance=NULL;

SourceManage::SourceManage()
{
    pNetSendReceive=new NetSendReceive;
    pNetDealwith=new NetDealwith(pNetSendReceive);

    Init();
}

void SourceManage::Init()
{
    bool state;
    state=FM->LoadFromFile(FileManage::GetAnalyteDataBaseFile(),&analyteDataBase);
    state=FM->LoadFromFile(FileManage::GetMassCalStdFile(),&massCalStandard);
    state=FM->LoadFromFile(FileManage::GetMassCalHtdFile(),&massCalHigh);
    state=FM->LoadFromFile(FileManage::GetCrossCalFile(),&crossCal);
    state=FM->LoadFromFile(FileManage::GetWorkFlowFile(),&workFlow);

    QDir dir(FileManage::GetTuneManualDir());
    foreach(QFileInfo mfi,dir.entryInfoList(QDir::Files))
    {
        TuneResult tuneResult;
        state=FM->LoadFromFile(mfi.filePath(),&tuneResult);
        if(state==true)
        {
           tuneResults.append(tuneResult);
        }
    }

    systemState.START_STOP=0;
    systemState.READY=0;

    QStringList strList;
    strList.clear();
    strList=QStringList()<< QObject::tr("标准")
                                     <<QObject::tr("高标");
    options.insert("ResolutionType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("无")<<QObject::tr("插值");//++
    options.insert("InternalSType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("ppq")<<QObject::tr("ppt")<<QObject::tr("ppb")<<QObject::tr("ppm")
                           <<QObject::tr("%")<<QObject::tr("mg/L")<<QObject::tr("ug/L")<<QObject::tr("pg/L");
    options.insert("UnitType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("无")<<QObject::tr("全定量")<<QObject::tr("标准加入法");//++
    options.insert("QuantitativeType",strList);
    strList=QStringList()<< QObject::tr("一次函数")
                                     <<QObject::tr("二次函数");
    options.insert("FittingTimesType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("无")<<QObject::tr("1/SD")<<QObject::tr("1/浓度")<<QObject::tr("过原点");//++
    options.insert("WeightType1",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("无")<<QObject::tr("1/SD")<<QObject::tr("过标准加入零点");//++
    options.insert("WeightType2",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("空白")<<QObject::tr("全定量标准")<<QObject::tr("未知样品")
                           <<QObject::tr("标准加入标准")<<QObject::tr("标准加入零标准")<<QObject::tr("QC样品");
    options.insert("SampleType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("无")<<QObject::tr("IVC初始曲线检查")<<QObject::tr("CCV连续曲线检查")
                           <<QObject::tr("QCS质量控制样品")<<QObject::tr("LCS实验室控制样品")<<QObject::tr("ICB初始空白检查");
    options.insert("QcType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr(">")<<QObject::tr("<")<<QObject::tr("=")
                           <<QObject::tr(">=")<<QObject::tr("<=");
    options.insert("SymbleType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("放空")<<QObject::tr("真空就绪")<<QObject::tr("分析就绪");
    options.insert("StateType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("打开")<<QObject::tr("关闭");
    options.insert("OnOffType",strList);
    strList.clear();
    strList=QStringList()<<QObject::tr("顺时针")<<QObject::tr("逆时针");
    options.insert("DirectType",strList);
}

AnalyteDataBase* SourceManage::GetAnalyteDataBase()
{
  return &analyteDataBase;
}
CalRatio SourceManage::GetCalStandard()
{
    return this->massCalStandard;
}
CalRatio SourceManage::GetCalHigh()
{
    return this->massCalHigh;
}
CalRatio SourceManage::GetCalCross()
{
    return this->crossCal;
}
QList<TuneResult> SourceManage::GetTuneResults()
{
    return this->tuneResults;
}
bool SourceManage::SetTune(int index,bool state)
{
    if(index<0) return false;
    int count=tuneResults.count();
    if(count<=index) return false;

    for(int i=0;i<count;i++)
    {
        if(index==i)
        {
            tuneResults[i].bIsChoose=state;
        }
        else
        {
            if(state==true)//
            {
                tuneResults[i].bIsChoose=false;
            }
        }
    }

    return true;
}
QStringList SourceManage::GetOption(QString name)
{
    //return this->options.values(name);
}
void SourceManage::AddOpenedExps(QString path,Experiment exp)
{
    openedExps.insert(path,exp);
}
void SourceManage::RemoveOpenedExps(QString path)
{
    openedExps.remove(path);
}
Experiment* SourceManage::GetOpenedExps(QString path)
{
    return &openedExps[path];
}
QueueExp *SourceManage::GetQueueExp()
{
    return &queueExps;
}
WorkFlow SourceManage::GetWorkFlow()
{
  return workFlow;
}
WorkFlow *SourceManage::GetWorkFlowPtr()
{
    return &workFlow;
}
bool SourceManage::SetWorkFlow(WorkFlow flow)
{
    workFlow=flow;
//    workFlow.Clear();
//    foreach(QString name,flow.GetFlowNames())
//    {
//       workFlow.InsetFlow(name,flow.GetWorkSteps(name));
//    }
}
void SourceManage::SetSystemState(SystemState state)
{
    systemState=state;
}
SystemState SourceManage::GetSystemState()
{
    return systemState;
}
void SourceManage::SetWorkStep(WorkStep value)
{
   workStepSet=value;
}
WorkStep SourceManage::GetWorkStep()
{
    return workStepSet;
}
WorkStep *SourceManage::GetWorkStepePtr()
{
   return &workStepSet;
}
LimitValue SourceManage::GetLimitValue()
{
    return limitValue;
}
void SourceManage::SetLimitValue(LimitValue value)
{
    limitValue=value;
}
NetSendReceive *SourceManage::GetNetSendReceive()
{
    return pNetSendReceive;
}
NetDealwith *SourceManage::GetNetDealwith()
{
    return pNetDealwith;
}
